package cn.stylefeng.guns.modular.project.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.project.entity.ProjectMember;
import cn.stylefeng.guns.modular.project.mapper.ProjectMemberMapper;
import cn.stylefeng.guns.modular.project.mapper.ProjectMemberWorkMapper;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberParam;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberWorkParam;
import cn.stylefeng.guns.modular.project.model.result.ProjectMemberResult;
import cn.stylefeng.guns.modular.project.service.ProjectMemberService;
import cn.stylefeng.guns.modular.project.service.ProjectMemberWorkService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-10-15
 */
@Service
public class ProjectMemberServiceImpl extends ServiceImpl<ProjectMemberMapper, ProjectMember> implements ProjectMemberService {
    @Autowired
    private ProjectMemberWorkService projectMemberWorkService;
    @Autowired
    private ProjectMemberWorkMapper projectMemberWorkMapper;

    @Override
    public void add(ProjectMemberParam param) {
        ProjectMember entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProjectMemberParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProjectMemberParam param) {
        ProjectMember oldEntity = getOldEntity(param);
        ProjectMember newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ProjectMemberResult findBySpec(ProjectMemberParam param) {
        return null;
    }

    @Override
    public List<ProjectMemberResult> findListBySpec(ProjectMemberParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProjectMemberParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<ProjectMemberResult> records = page.getRecords();
        for (ProjectMemberResult projectMemberResult : records) {
            int sumWorkHour = projectMemberWorkService.sumWorkHour1(projectMemberResult.getProjectId(), projectMemberResult.getUserId());
            projectMemberResult.setSumWorkHour(sumWorkHour);
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findAllMember(ProjectMemberParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.findAllMember(pageContext, param);

        page.getRecords().forEach(consumer -> {
            Long userId = ((ProjectMemberResult) consumer).getUserId();
            ProjectMemberWorkParam paramCondition = new ProjectMemberWorkParam();
            paramCondition.setUserId(userId);
            String lastWorkDate = projectMemberWorkMapper.findLastWorkDate(paramCondition);
            ((ProjectMemberResult) consumer).setLastWorkDate(lastWorkDate);
        });

        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int countMember(int projectId) {
        return this.baseMapper.countMember(projectId);
    }

    @Override
    public List<ProjectMemberResult> findMyProjects() {
        Long userId = ShiroKit.getUser().getId();
        ProjectMemberParam paramCondition = new ProjectMemberParam();
        paramCondition.setUserId(userId);
        return this.baseMapper.findUserProjects(paramCondition);
    }

    private Serializable getKey(ProjectMemberParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ProjectMember getOldEntity(ProjectMemberParam param) {
        return this.getById(getKey(param));
    }

    private ProjectMember getEntity(ProjectMemberParam param) {
        ProjectMember entity = new ProjectMember();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
